//
//  SWHomeCell.swift
//  SinaWeibo
//
//  Created by wszyxc on 16/7/23.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

import UIKit

import SnapKit


//- 由于是列表 每条微博数据 使用cell显示
//    - 原创微博
//    - 头像
//    - 昵称
//    - 会员等级
//    - 认证
//    - 微博发布时间
//    - 微博来源
//    - 微博内容
//    - 配图(可能存在 可能不存在)
//    
//    - 转发微博 （可能存在, 可能不存在）
//- 微博内容
//- 配图(可能存在 可能不存在)
//
//- 底部视图
//- 转发按钮
//- 评论按钮
//- 赞按钮


class SWHomeCell: UITableViewCell {

    //底部视图的 top约束
    var bottomViewTopConstraint:Constraint?
    //原创微博的 bottom约束
    var originalViewBottomConstraint:Constraint?
    //转发微博的 bottom约束
    var retweetViewBottomConstraint:Constraint?
    
    var originalViewModel:SWOriginalViewModel?{
        
        didSet{
            
            //0.给底部视图赋值
            
            bottomView.originalViewModel = originalViewModel
            
            //1.给原创微博赋值
            origView.originalViewModel = originalViewModel

            //解除旧约束
            bottomViewTopConstraint?.uninstall()
            originalViewBottomConstraint?.uninstall()
            retweetViewBottomConstraint?.uninstall()
            
            //如果原创微博有照片,则拿原创微博的照片做底部约束
            if originalViewModel?.homeModel?.pic_urls?.count>0{
                
                origView.snp_updateConstraints(closure: { (make) -> Void in
                    
                  self.originalViewBottomConstraint =  make.bottom.equalTo(origView.pictureView.snp_bottom).offset(ItemMargin).constraint
                    
                })
                
            }else{

                //如果原创微博没有照片,则拿原创微博的内容做底部约束
                origView.snp_makeConstraints(closure: { (make) -> Void in
    
                 self.originalViewBottomConstraint = make.bottom.equalTo(origView.contentLabel.snp_bottom).offset(ItemMargin).constraint
                })
                
            }
            
            
            //如果有转发微博,bottomView就相对于转发微博做约束

            if originalViewModel?.homeModel?.retweeted_status != nil{
                
                //如果有转发微博  先赋值
                //2.给转发微博赋值
                retweetView.retWeetModel = originalViewModel?.homeModel?.retweeted_status
     
                retweetView.hidden = false
                
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    
                   self.bottomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
                })
                
                //如果有转发微博的照片,转发微博的底部约束就相对于照片做
                if  originalViewModel?.homeModel?.retweeted_status?.pic_urls?.count>0{
                    
                    retweetView.snp_updateConstraints(closure: { (make) -> Void in
                        
                        self.retweetViewBottomConstraint =  make.bottom.equalTo(retweetView.pictureView.snp_bottom).offset(ItemMargin).constraint
                    })
                    
                }else{
                    retweetView.snp_updateConstraints(closure: { (make) -> Void in
                        
                       self.retweetViewBottomConstraint = make.bottom.equalTo(retweetView.contentLabel.snp_bottom).offset(ItemMargin).constraint
                    })
                    
                }
    
                
            }else{
                
                //如果没有转发微博,隐藏转发微博,底部视图的约束就相对于原创微博做
                retweetView.hidden = true
                
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    
                  self.retweetViewBottomConstraint = make.top.equalTo(origView.snp_bottom).constraint
                })

     
            }
 
        }
        
    }
    
    
    /*  
        //很明显,这两个为约束冲突项
    //
    "< SinaWeibo.SWBottomView:0x7fd812ea2710.top == SinaWeibo.SWOriginalView:0x7fd812e93160.bottom + 10.0>",
    //
    "< SinaWeibo.SWBottomView:0x7fd812ea2710.top == SinaWeibo.SWRetweetView:0x7fd812e9e960.bottom + 10.0>",
    
    */
    

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI(){
        
        //添加原创微博
        contentView.addSubview(origView)
        
        origView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.top.equalTo(contentView)

            //相对于内部控件的底部约束 统统在外面做
            originalViewBottomConstraint =  make.bottom.equalTo(origView.pictureView).offset(ItemMargin).constraint
            
        }
        
        //添加转发微博

        contentView.addSubview(retweetView)
        
        retweetView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(origView.snp_bottom)
            
            make.leading.trailing.equalTo(contentView)
            
            //相对于内部控件的底部约束 统统在外面做
           retweetViewBottomConstraint = make.bottom.equalTo(retweetView.pictureView).offset(ItemMargin).constraint
            
        }

        //添加底部视图
        contentView.addSubview(bottomView)
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.bottom.equalTo(contentView)
            make.height.equalTo(35)
            bottomViewTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
        //    make.top.equalTo(origView.snp_bottom)
        }
        
        //设置contentView的约束
        
//        contentView.snp_makeConstraints { (make) -> Void in
//            
//            make.top.equalTo(origView.snp_top)
//            make.bottom.equalTo(bottomView.snp_bottom)
//        }
        
    }
    
    //懒加载控件
    
    //1.懒加载原创view
    private lazy var origView:SWOriginalView = SWOriginalView()
    
    //2.懒加载bottomView
    private lazy var bottomView:SWBottomView = SWBottomView()
    
    //3.懒加载retweetView
    
    private lazy var retweetView:SWRetweetView = SWRetweetView()
    

}


